﻿using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace Sidvall.IdentityManager.Data
{
	public static class CodeManager
	{
		#region Public Members

		#region GetClassNamesAsync

		public static async Task<string[]> GetClassNamesAsync(string path)
		{
			HashSet<string> list;
			string[] classNames;
			IEnumerable<string> files;
			Assembly assembly;

			list = new HashSet<string>();
			files = await Sidvall.SystemContext.Current.SystemStorageManager.GetFileNamesAsync(path, new string[] { "*.dll" }, IO.SearchOption.TopDirectoryOnly).ConfigureAwait(false);
			foreach (var file in files)
			{
				assembly = await Sidvall.SystemContext.Current.CodeManager.GetAssemblyAsync(file);
				classNames = Sidvall.SystemContext.Current.CodeManager.GetAssemblyTypeNames(assembly, true, false, true, typeof(Sidvall.IdentityManager.IPlugin).GetTypeInfo());
				if (classNames != null)
				{
					foreach (var className in classNames)
					{
						if (!list.Contains(className))
							list.Add(className);
					}
				}
			}
			return list.ToArray();
		}

		#endregion

		#endregion
	}
}
